require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'select2': '/js/lib/select2.min',
        'bootstrap': '/js/lib/bootstrap.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

var myFun = {};
require(['jquery', 'doT', 'select2', 'common', 'bootstrap'], function($, doT, select2, common) {
    $(function() {
        function initSchool() {
            $.get(common.urlBase + "/school/allSchool.json", function(data, statu) {
                data = eval('(' + data + ')');
                myFun.schools = data.data;
                var schoolTmpl = doT.template($('#template_school').text());
                $('#school').html(schoolTmpl(myFun.schools));
                $('#school').select2();
                initMaster();
            });
        }
        initSchool();

        function getId() {
            var p = location.search.split('?')[1];
            var items = p.split('&');
            myFun.item = {};
            $.each(items, function() {
                var key = this.split('=')[0];
                var value = this.split('=')[1];
                myFun.item[key] = value;
            });
        }
        getId();

        var id = myFun.item['id']; //获取要修改的堂主的ID
        function initMaster() {
            $.get(common.urlBase + '/account/get/' + id + '.json', function(data, statu) {
                var data = eval('(' + data + ')');
                if (data['ok']) {
                    for (var d in data.data) {
                        if (d != 'id' && d != 'gender') {
                            $('#' + d).val(data.data[d]);
                        }
                    }

                    $('#master_name').val(data.data['name']);
                    data.data['gender'] == '2' ? $('#female').attr("checked", "checked") : $('#male').attr("checked", "checked");

                    var school = data.data['school_id'];
                    $('#school').val(school);
                    $.each(myFun.schools, function(index, value) {
                        if (school == this.id) {
                            $('#school').val(this.id);
                            $('#select2-school-container').text(myFun.schools[index].name);

                        }
                    });

                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }
            });
        }
        

        $('#change_master').submit(function() {
            var account = $('#account').val();
            var pwd = $('#password').val();
            var name = $('#master_name').val();
            var genderId = $('input:radio:checked').attr('id');
            var gender = genderId == 'male' ? 1 : 2;
            var phone = $('#phone').val();
            var email = $('#email').val();
            var school_id = $('#school').val();
            var role = 2;

            var data = {
                'id': id,
                'name': name,
                'gender': gender,
                'account': account,
                'password': pwd,
                'phone': phone,
                'email': email,
                'role': role,
                'school_id': school_id
            };

            $.post(common.urlBase + '/account/edit.json', data, function(data, statu) {
                data = eval('(' + data + ')');
                if (data['ok']) {
                    $('#noticeMsg').text('修改成功！');
                    $('#noticeModal').modal('show');
                    window.location = '/views/master.html';
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }
            });
            return false;
        });



        $('.sidebar ul li').each(function() {
            if ($(this).text() == '查看堂主') {
                $(this).parent().css('display', 'block');
                $(this).children().css('color', '#46A3FF');
            }
        });
    });


});
